package com.ttl.bc.ap.invoice.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderShowBean;
import com.ttl.common.util.CommonUtils;

public class BCAPInvoice2Form extends ActionForm {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4812941335278054733L;
	
	private String[]	docNoArray;		//สำหรับเก็บค่า docNo ที่ถูกเลือกมาเพื่อทำ Invoice
		
	private String		preDocNo;	
	private Integer		billType;
	private Integer		taxType;
	private	String		docNo;
	private	String		docDate;
	private	String		taxNo;
	private	String		taxDate;
	private	String		apCode;	
	private	String		apName;
	private	String		contactCode;
	private	String		sumOfItemAmount;		
	private String		departCode;
	private	String		projectCode;
	private	String		discountAmount;
	private	String		allocateCode;
	private	String		statementDate;
	private	String		taxAmount;
	private	String		creditDay;
	private	String		dueDate;
	private	String		totalAmount;	
	private	String		billGroup;
	private	String		taxRate;
	private	String		exceptTaxAmount;	
	private	String		currencyCode;
	private	String		exchangeRate;
	private	String		netAmount;	
	private String		creatorCode;	
	private	String		payAmount;
	private	String		refNo;	
	
	private	String		myDescription;
	private String		ittl_payment;
	
	private	String[]	ittl_purchaseCode;
	private	String[]	ittl_rowOrderOfStkReq;
	private	String[]	lineNumber;
	private	String[]	itemCode;			
	private	String[]	itemName;
	private	String[]	lineItem_whCode;
	private	String[]	lineItem_shelfCode;
	private	String[]	unitCode;
	private	String[]	unitName;
	private	String[]	qty;
	private String[]	price;
	private String[]	lineItemDiscountAmount;
	private	String[]	amount;
	private	String[]	poRefNo;
	private	String[]	stkReqNo;   //เลขที่ใบเสนอซื้อ (ขอซื้อ)
	private	String[]	confirmNo;	//เลขที่ใบอนุมัติเสนอซื้อ
	
	private String[]	lineItem_statusReceive;
	private String[]	lineItem_departCode;
	private String[]	lineItem_projectCode;
	private String[]	lineItem_allocateCode;
	private String[]	lineItem_myDescription;
	private String[]	lineItem_poRefNo;
	private String[]	lineItem_netAmount;
	private String[]	lineItem_homeAmount;
	
	@SuppressWarnings("unchecked")
	private	ArrayList	invoiceSub;
	
	
	
	public String[] getIttl_purchaseCode() {
		return ittl_purchaseCode;
	}

	public void setIttl_purchaseCode(String[] ittlPurchaseCode) {
		ittl_purchaseCode = ittlPurchaseCode;
	}

	public String[] getIttl_rowOrderOfStkReq() {
		return ittl_rowOrderOfStkReq;
	}

	public void setIttl_rowOrderOfStkReq(String[] ittlRowOrderOfStkReq) {
		ittl_rowOrderOfStkReq = ittlRowOrderOfStkReq;
	}

	public String getIttl_payment() {
		return ittl_payment;
	}

	public void setIttl_payment(String ittlPayment) {
		ittl_payment = ittlPayment;
	}

	public String getMyDescription() {
		return myDescription;
	}

	public void setMyDescription(String myDescription) {
		this.myDescription = myDescription;
	}

	public String getApName() {
		return apName;
	}

	public void setApName(String apName) {
		this.apName = apName;
	}

	public String[] getLineNumber() {
		return lineNumber;
	}

	public void setLineNumber(String[] lineNumber) {
		this.lineNumber = lineNumber;
	}

	public String[] getDocNoArray() {
		return docNoArray;
	}

	public void setDocNoArray(String[] docNoArray) {
		this.docNoArray = docNoArray;
	}

	public String getPreDocNo() {
		return preDocNo;
	}

	public void setPreDocNo(String preDocNo) {
		this.preDocNo = preDocNo;
	}

	public Integer getBillType() {
		return billType;
	}

	public void setBillType(Integer billType) {
		this.billType = billType;
	}

	public Integer getTaxType() {
		return taxType;
	}

	public void setTaxType(Integer taxType) {
		this.taxType = taxType;
	}

	public String getDocNo() {
		return docNo;
	}

	public void setDocNo(String docNo) {
		this.docNo = docNo;
	}

	public String getDocDate() {
		return docDate;
	}

	public void setDocDate(String docDate) {
		this.docDate = docDate;
	}

	public String getTaxNo() {
		return taxNo;
	}

	public void setTaxNo(String taxNo) {
		this.taxNo = taxNo;
	}

	public String getTaxDate() {
		return taxDate;
	}

	public void setTaxDate(String taxDate) {
		this.taxDate = taxDate;
	}

	public String getApCode() {
		return apCode;
	}

	public void setApCode(String apCode) {
		this.apCode = apCode;
	}

	public String getContactCode() {
		return contactCode;
	}

	public void setContactCode(String contactCode) {
		this.contactCode = contactCode;
	}

	public String getSumOfItemAmount() {
		return sumOfItemAmount;
	}

	public void setSumOfItemAmount(String sumOfItemAmount) {
		this.sumOfItemAmount = sumOfItemAmount;
	}

	public String getDepartCode() {
		return departCode;
	}

	public void setDepartCode(String departCode) {
		this.departCode = departCode;
	}

	public String getProjectCode() {
		return projectCode;
	}

	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	public String getDiscountAmount() {
		return discountAmount;
	}

	public void setDiscountAmount(String discountAmount) {
		this.discountAmount = discountAmount;
	}

	public String getAllocateCode() {
		return allocateCode;
	}

	public void setAllocateCode(String allocateCode) {
		this.allocateCode = allocateCode;
	}

	public String getStatementDate() {
		return statementDate;
	}

	public void setStatementDate(String statementDate) {
		this.statementDate = statementDate;
	}

	public String getTaxAmount() {
		return taxAmount;
	}

	public void setTaxAmount(String taxAmount) {
		this.taxAmount = taxAmount;
	}

	public String getCreditDay() {
		return creditDay;
	}

	public void setCreditDay(String creditDay) {
		this.creditDay = creditDay;
	}

	public String getDueDate() {
		return dueDate;
	}

	public void setDueDate(String dueDate) {
		this.dueDate = dueDate;
	}

	public String getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(String totalAmount) {
		this.totalAmount = totalAmount;
	}

	public String getBillGroup() {
		return billGroup;
	}

	public void setBillGroup(String billGroup) {
		this.billGroup = billGroup;
	}

	public String getTaxRate() {
		return taxRate;
	}

	public void setTaxRate(String taxRate) {
		this.taxRate = taxRate;
	}

	public String getExceptTaxAmount() {
		return exceptTaxAmount;
	}

	public void setExceptTaxAmount(String exceptTaxAmount) {
		this.exceptTaxAmount = exceptTaxAmount;
	}

	public String getCurrencyCode() {
		return currencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		this.currencyCode = currencyCode;
	}

	public String getExchangeRate() {
		return exchangeRate;
	}

	public void setExchangeRate(String exchangeRate) {
		this.exchangeRate = exchangeRate;
	}

	public String getNetAmount() {
		return netAmount;
	}

	public void setNetAmount(String netAmount) {
		this.netAmount = netAmount;
	}

	public String getCreatorCode() {
		return creatorCode;
	}

	public void setCreatorCode(String creatorCode) {
		this.creatorCode = creatorCode;
	}

	public String getPayAmount() {
		return payAmount;
	}

	public void setPayAmount(String payAmount) {
		this.payAmount = payAmount;
	}

	public String getRefNo() {
		return refNo;
	}

	public void setRefNo(String refNo) {
		this.refNo = refNo;
	}

	public String[] getItemCode() {
		return itemCode;
	}

	public void setItemCode(String[] itemCode) {
		this.itemCode = itemCode;
	}

	public String[] getItemName() {
		return itemName;
	}

	public void setItemName(String[] itemName) {
		this.itemName = itemName;
	}

	public String[] getLineItem_whCode() {
		return lineItem_whCode;
	}

	public void setLineItem_whCode(String[] lineItemWhCode) {
		lineItem_whCode = lineItemWhCode;
	}

	public String[] getLineItem_shelfCode() {
		return lineItem_shelfCode;
	}

	public void setLineItem_shelfCode(String[] lineItemShelfCode) {
		lineItem_shelfCode = lineItemShelfCode;
	}

	public String[] getLineItem_statusReceive() {
		return lineItem_statusReceive;
	}

	public void setLineItem_statusReceive(String[] lineItemStatusReceive) {
		lineItem_statusReceive = lineItemStatusReceive;
	}

	public String[] getLineItem_departCode() {
		return lineItem_departCode;
	}

	public void setLineItem_departCode(String[] lineItemDepartCode) {
		lineItem_departCode = lineItemDepartCode;
	}

	public String[] getLineItem_projectCode() {
		return lineItem_projectCode;
	}

	public void setLineItem_projectCode(String[] lineItemProjectCode) {
		lineItem_projectCode = lineItemProjectCode;
	}

	public String[] getLineItem_allocateCode() {
		return lineItem_allocateCode;
	}

	public void setLineItem_allocateCode(String[] lineItemAllocateCode) {
		lineItem_allocateCode = lineItemAllocateCode;
	}

	public String[] getLineItem_myDescription() {
		return lineItem_myDescription;
	}

	public void setLineItem_myDescription(String[] lineItemMyDescription) {
		lineItem_myDescription = lineItemMyDescription;
	}

	public String[] getLineItem_poRefNo() {
		return lineItem_poRefNo;
	}

	public void setLineItem_poRefNo(String[] lineItemPoRefNo) {
		lineItem_poRefNo = lineItemPoRefNo;
	}

	public String[] getLineItem_netAmount() {
		return lineItem_netAmount;
	}

	public void setLineItem_netAmount(String[] lineItemNetAmount) {
		lineItem_netAmount = lineItemNetAmount;
	}

	public String[] getLineItem_homeAmount() {
		return lineItem_homeAmount;
	}

	public void setLineItem_homeAmount(String[] lineItemHomeAmount) {
		lineItem_homeAmount = lineItemHomeAmount;
	}

	public String[] getUnitCode() {
		return unitCode;
	}

	public void setUnitCode(String[] unitCode) {
		this.unitCode = unitCode;
	}

	public String[] getUnitName() {
		return unitName;
	}

	public void setUnitName(String[] unitName) {
		this.unitName = unitName;
	}

	public String[] getQty() {
		return qty;
	}

	public void setQty(String[] qty) {
		this.qty = qty;
	}

	public String[] getPrice() {
		return price;
	}

	public void setPrice(String[] price) {
		this.price = price;
	}

	public String[] getLineItemDiscountAmount() {
		return lineItemDiscountAmount;
	}

	public void setLineItemDiscountAmount(String[] lineItemDiscountAmount) {
		this.lineItemDiscountAmount = lineItemDiscountAmount;
	}

	public String[] getAmount() {
		return amount;
	}

	public void setAmount(String[] amount) {
		this.amount = amount;
	}

	public String[] getPoRefNo() {
		return poRefNo;
	}

	public void setPoRefNo(String[] poRefNo) {
		this.poRefNo = poRefNo;
	}

	public String[] getStkReqNo() {
		return stkReqNo;
	}

	public void setStkReqNo(String[] stkReqNo) {
		this.stkReqNo = stkReqNo;
	}

	public String[] getConfirmNo() {
		return confirmNo;
	}

	public void setConfirmNo(String[] confirmNo) {
		this.confirmNo = confirmNo;
	}

	@SuppressWarnings("unchecked")
	public ArrayList getInvoiceSub() {
		return invoiceSub;
	}

	@SuppressWarnings("unchecked")
	public void setInvoiceSub(ArrayList invoiceSub) {
		this.invoiceSub = invoiceSub;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@SuppressWarnings("unchecked")
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		
		String param = ""+(String)request.getParameter("param");
		
		if(param.equals("New") || param.equals("Update") ) {			
			
			if (getDocNo() == null || getDocNo().length()<1) {
				errors.add("docNo", new ActionMessage("error.apinvoice.docNo.required"));						
			}
			
			if (getDocDate() == null || getDocDate().length()<1) {
				errors.add("docDate", new ActionMessage("error.apinvoice.docDate.required"));
			}else if(CommonUtils.isValidDate(getDocDate())==false) {
				errors.add("docDate", new ActionMessage("error.apinvoice.docDate.invalid"));
			}
			
			if (getTaxNo() != null && getTaxNo().trim().length()>0) {
				
				if (getTaxDate() == null || getTaxDate().length()<1) {
					errors.add("taxDate", new ActionMessage("error.apinvoice.taxDate.required"));				
				}else if(CommonUtils.isValidDate(getTaxDate())==false) {
					errors.add("taxDate", new ActionMessage("error.apinvoice.taxDate.invalid"));				
				}
			}
			
			if (getTaxDate() != null && getTaxDate().length()>0) {
				
				if(CommonUtils.isValidDate(getTaxDate())==false) {
					errors.add("taxDate", new ActionMessage("error.apinvoice.taxDate.invalid"));
//				}else if ( getTaxNo() == null || getTaxNo().length()<1) {
//					errors.add("taxNo", new ActionMessage("error.apinvoice.taxNo.required"));				
				}
			}		
	
			if (getApCode() == null || getApCode().length()<1) {
				errors.add("apCode", new ActionMessage("error.apinvoice.apCode.required"));						
			}
			
/*			if (getIttl_payment() == null || getIttl_payment().length()<1) {
				errors.add("ittl_payment", new ActionMessage("error.apinvoice.ittl_payment.required"));						
			}*/			
			
			if (getSumOfItemAmount() == null || getSumOfItemAmount().length() < 0 || CommonUtils.isNumber(getSumOfItemAmount())==false
					|| (Double.parseDouble(getSumOfItemAmount())<=0) ) 
				errors.add("sumOfItemAmount", new ActionMessage("error.apinvoice.sumOfItemAmount.invalid"));
			
			if (getDepartCode() == null || getDepartCode().length()<1) {
				errors.add("departCode", new ActionMessage("error.apinvoice.departCode.required"));						
			}		
			
			if (getDiscountAmount() == null || getDiscountAmount().length() < 0 || CommonUtils.isNumber(getDiscountAmount())==false
					|| (Double.parseDouble(getDiscountAmount())<0) )
				setDiscountAmount("0.0");
//				errors.add("discountAmount", new ActionMessage("error.apinvoice.discountAmount.invalid"));		
			
			if (getStatementDate() == null || getStatementDate().length()==0 && CommonUtils.isValidDate(getStatementDate())==false) {
				errors.add("statementDate", new ActionMessage("error.apinvoice.statementDate.invalid"));			
			}		
	
			if (getTaxAmount() == null || getTaxAmount().length() < 0 || CommonUtils.isNumber(getTaxAmount())==false
					|| (Double.parseDouble(getTaxAmount())<0) ) 
				errors.add("taxAmount", new ActionMessage("error.apinvoice.taxAmount.invalid"));		
						
//			if (getDueDate() != null && getDueDate().length()>0 && CommonUtils.isValidDate(getDueDate())==false) {
//				errors.add("dueDate", new ActionMessage("error.apinvoice.dueDate.invalid"));			
//			}
			if (getDueDate() == null || getDueDate().length()==0 || CommonUtils.isValidDate(getDueDate())==false) {
				errors.add("dueDate", new ActionMessage("error.apinvoice.dueDate.invalid"));			
			}			
			
			if(CommonUtils.isValidDate(getStatementDate() ) && CommonUtils.isValidDate(getDueDate())) {
				Date date1 = CommonUtils.convertStringToDate(getStatementDate());
				Date date2 = CommonUtils.convertStringToDate(getDueDate());								
				
				if(date2.getTime()-date1.getTime()<0) {
					System.out.println("date2.getTime()-date1.getTime()="+(date2.getTime()-date1.getTime()));
					errors.add("dueDate", new ActionMessage("error.apinvoice.dueDate.invalid"));	
				}
				setCreditDay(""+(int)(date2.getTime()-date1.getTime())/(1000*60*60*24));
			}
			
			if (getCreditDay() == null || getCreditDay().length() < 0 || CommonUtils.isNumber(getCreditDay())==false
					|| (Double.parseDouble(getCreditDay())<0) )
				setCreditDay("0");
				//errors.add("creditDay", new ActionMessage("error.apinvoice.creditDay.invalid"));			
			
			
			if (getTotalAmount() == null || getTotalAmount().length() < 0 || CommonUtils.isNumber(getTotalAmount())==false
					|| (Double.parseDouble(getTotalAmount())<=0) ) 
				errors.add("totalAmount", new ActionMessage("error.apinvoice.totalAmount.invalid"));		
			
			if (getTaxRate() == null || getTaxRate().length() <= 0 || CommonUtils.isNumber(getTaxRate())==false
					|| (Double.parseDouble(getTaxRate())<0) )
				setTaxRate("7.00");
				//errors.add("taxRate", new ActionMessage("error.apinvoice.taxRate.invalid"));		
			
			if (getExceptTaxAmount() == null || getExceptTaxAmount().length() < 0 || CommonUtils.isNumber(getExceptTaxAmount())==false
					|| (Double.parseDouble(getExceptTaxAmount())<0) )
				setExceptTaxAmount("0.00");
				//errors.add("exceptTaxAmount", new ActionMessage("error.apinvoice.exceptTaxAmount.invalid"));		
			
			
			if (getExchangeRate() == null || getExchangeRate().length() <= 0 || CommonUtils.isNumber(getExchangeRate())==false
					|| (Double.parseDouble(getExchangeRate())<0) ) 			
				setExchangeRate("1.00");
				//errors.add("exchangeRate", new ActionMessage("error.apinvoice.exchangeRate.invalid"));
			
	
			if (getNetAmount() == null || getNetAmount().length() < 0 || CommonUtils.isNumber(getNetAmount())==false
					|| (Double.parseDouble(getNetAmount())<0) ) 
				errors.add("netAmount", new ActionMessage("error.apinvoice.netAmount.invalid"));
			
			if(getDocNo()!=null && getDocNo().length()>=3 && getDocNo().substring(0, 3).equalsIgnoreCase("BCS")) {
				if (getPayAmount() == null || getPayAmount().length() < 0 || CommonUtils.isNumber(getPayAmount())==false
						|| (Double.parseDouble(getPayAmount())<=0) ) 
					errors.add("payAmount", new ActionMessage("error.apinvoice.payAmount.invalid"));
				
				if (getRefNo() == null ) 
					errors.add("refNo", new ActionMessage("error.apinvoice.refNo.invalid"));
			}
			
			ArrayList resultList1 = new ArrayList();
						
			if(getItemCode()!=null) {								
				
				request.setAttribute("countId", getLineNumber()[getLineNumber().length-1]);
				  												
				for (int i = 0; i < getItemCode().length; i++) {										
					
					if (getIttl_rowOrderOfStkReq()[i] == null || getIttl_rowOrderOfStkReq()[i].length() < 1) 
						setIttl_rowOrderOfStkReq(ittl_rowOrderOfStkReq);
					
					if (getItemCode()[i] == null || getItemCode()[i].length() < 1) 
						errors.add("itemCode"+(i+1), new ActionMessage("error.apinvoice.itemCode.required"));						
					
					if (getItemName()[i] == null || getItemName()[i].length() < 1) 
						errors.add("itemName"+(i+1), new ActionMessage("error.apinvoice.itemName.required"));						
										
					if (getQty()[i] == null || getQty()[i].length() < 0 || CommonUtils.isNumber(getQty()[i])==false
							|| (Double.parseDouble(getQty()[i])<=0) ) 
						errors.add("qty"+(i+1), new ActionMessage("error.apinvoice.qty.invalid"));
											
					if (getPrice()[i] == null || getPrice()[i].length() <= 0 || CommonUtils.isNumber(getPrice()[i])==false
							|| (Double.parseDouble(getPrice()[i])<0) ) 
						errors.add("price"+(i+1), new ActionMessage("error.apinvoice.price.invalid"));				
											
					System.out.println("getLineItemDiscountAmount()[i]==null ->"+(getLineItemDiscountAmount()[i]==null));
					System.out.println("getLineItemDiscountAmount()[i].length() < 0 ->"+(getLineItemDiscountAmount()[i].length() < 0));
					if (getLineItemDiscountAmount()[i]==null || getLineItemDiscountAmount()[i].length() < 0 ){						
						errors.add("lineItemDiscountAmount"+(i+1), new ActionMessage("error.apinvoice.lineItemDiscountAmount.invalid"));
						
					}else if(CommonUtils.isNumberPercent(getLineItemDiscountAmount()[i])==false) {//ตรวจว่า ค่าที่ใส่มานั้นเป็น % หรือไม่  -> ถ้าไม่เป็น percent จะตรวจต่อว่าเป็ฯตัวเลขป่าว 												
						
						if(CommonUtils.isNumber(getLineItemDiscountAmount()[i])==false) {//ถ้าไม่เป็นตัวเลขจะ invalid
							errors.add("lineItemDiscountAmount"+(i+1), new ActionMessage("error.apinvoice.lineItemDiscountAmount.invalid"));
						}
					}
											
					if (getAmount()[i] == null || getAmount()[i].length() < 0 || CommonUtils.isNumber(getAmount()[i])==false 
							|| (Double.parseDouble(getAmount()[i])<0) ) {
						errors.add("amount"+(i+1), new ActionMessage("error.apinvoice.amount.invalid"));						
					}
												
					/* ###################################################################### 
					 * ครั้งแรกของ resultList1 เป็นการดึงข้อมูลจากในDB มาแสดง
					 * ด้านล่างนี้เป็นการเอาค่าใน line item ปัจจุบันเก็บไว้  		
					 * ######################################################################*/			
					ArrayList al = (ArrayList)request.getSession().getAttribute("resultList1");
										
					if(al!=null && (i+1)<=al.size()) {
						boolean temp = false;
						for (int j = 0; j < al.size(); j++) {					
													
							BCPurchaseOrderShowBean bean = (BCPurchaseOrderShowBean)al.get(j);
							if(bean.getLineNumber().equals(getLineNumber()[i])) {
								System.out.println("กด enter เข้ามาเลยแถวนั้นอยู่ใน list อยู่ก่อนแล้ว");
								temp = true;																	
								resultList1.add(getBCPurchaseOrderShowBean(i));
								break;
							}						
						}						
						// กรณี ถ้าเพิ่มข้อมูลใหม่(ซึ่งสามารถลบได้) แล้ว enter เข้ามาเก็บไว้ใน list แล้วที่หน้าบันทึกลบข้อมูลใหม่ที่อยู่ใน list ออก แล้วเพิ่มข้อมูลใหม่อีกครั้ง(ตัวที่ยังไม่อยู่ใน list)
						// หรือหมายความว่าข้อมูลนั้นจะหาไม่เจอใน list จะมาเพิ่มใน if นี้
						if(temp==false) {//เป็น line item แถวใหม่ แต่ข้อมูลนั้นไม่มีอยู่ใน list (เป็นแถวที่ i ที่ <=ขนาด size ใน list)
							System.out.println("กด enter เข้ามาเลยแถวนั้นเป็นแถวใหม่ไม่อยู่ใน list ");
							resultList1.add(getBCPurchaseOrderShowBean(i));							
						}						
					}else {// ถ้าเป็น line item ใหม่โดยแถวข้อมูลนั้นมีมากกว่าใน list เดิมให้เก็บไว้
						System.out.println("กด enter เข้ามาเลยแถวนั้นเป็นแถวใหม่ไม่อยู่ใน list (ขนาดมากกว่า size)");
						resultList1.add(getBCPurchaseOrderShowBean(i));												
					}																																	
					// กรณี error :ค่ามันอยู่ใน actionForm ตอนเข้ามาใหม่ เช่น เลือก พ.1 ได้รายการมา 3 รายการ แล้วกด enter ข้อมูล lineitem จะถูกส่งให้ actionForm เก็บไว้แล้ว
					// (โดยข้อมูลไม่ได้อยู่ใน arrayList แต่จะอยู่ใน ActionForm หลังการกด enter)
					//	ถ้าไม่มี lineitem ใดๆเลย แล้วกด enter มันจะเอาค่าใน ActionForm เดิมมาแสดง 
					// ที่เป็นเช่นนี้เพราะเราไปกำหนด struts config ของ path นั้นด้วย  scope=session ต้องแก้ให้เป็น scope=request
					/* ################################# end ################################# */														
				}				
				
			}else {// ถ้ากดเข้ามาแล้ว ค้นหาในระบบพบว่า ไม่มีข้อมูล พ.1 ในระบบเลย->ให้แสดงerror และให้ใส่ line item อย่างน้อย 1 รายการ
				System.out.println("กด enter เข้ามาเลยโดยไม่มี line item");
				errors.add("itemCode1", new ActionMessage("error.apinvoice.itemCode.required"));						
				errors.add("itemName1", new ActionMessage("error.apinvoice.itemName.required"));						
				errors.add("qty1", new ActionMessage("error.apinvoice.qty.invalid"));
				errors.add("price1", new ActionMessage("error.apinvoice.price.invalid"));				
				errors.add("lineItemDiscountAmount1", new ActionMessage("error.apinvoice.lineItemDiscountAmount.invalid"));				
				errors.add("amount1", new ActionMessage("error.apinvoice.amount.invalid"));				
				
				request.setAttribute("countId", "1");
				
				BCPurchaseOrderShowBean bean = new BCPurchaseOrderShowBean();				
				bean.setLineNumber("1");
				bean.setStkReqNo("new line item");//ต้องกำหนดตัวนี้ด้วย เพราะใช้เป็น flag ในการมีเครื่องหมายลบ														
				resultList1.add(bean);								 										
			}
			
//			request.getSession().setAttribute("resultList1", resultList1);
			request.setAttribute("resultList_prpo", resultList1);
			
			
			//ควร add ลง bean ซักตัวหนึ่งจะดีกว่านี้ แล้วค่อย  setAttribute
			request.setAttribute("billType", getBillType());
			request.setAttribute("taxType", getTaxType());
			request.setAttribute("docNo", getDocNo());
			request.setAttribute("docDate", getDocDate());
			request.setAttribute("taxNo", getTaxNo());
			request.setAttribute("taxDate", getTaxDate());
			request.setAttribute("apCode", getApCode());
			
			if(getApName()!=null){
				try {
					request.setAttribute("apName", new String(getApName().getBytes("ISO8859_1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {e.printStackTrace();}
			}
			
			request.setAttribute("contactCode", getContactCode());
			request.setAttribute("sumOfItemAmount", getSumOfItemAmount());
			request.setAttribute("departCode", getDepartCode());
			request.setAttribute("projectCode", getProjectCode());
			request.setAttribute("discountAmount", getDiscountAmount());
			request.setAttribute("allocateCode", getAllocateCode());
			request.setAttribute("statementDate", getStatementDate());
			request.setAttribute("taxAmount", getTaxAmount());
			request.setAttribute("creditDay", getCreditDay());
			request.setAttribute("dueDate",  getDueDate());
			request.setAttribute("totalAmount", getTotalAmount());
			request.setAttribute("billGroup", getBillGroup());
			request.setAttribute("taxRate", getTaxRate());
			request.setAttribute("exceptTaxAmount", getExceptTaxAmount());
			request.setAttribute("currencyCode", getCurrencyCode());
			request.setAttribute("exchangeRate", getExchangeRate());
			request.setAttribute("netAmount", getNetAmount());
			request.setAttribute("payAmount", getPayAmount());
			request.setAttribute("refNo", getRefNo());
		}
		
		return errors;
	}

	private BCPurchaseOrderShowBean getBCPurchaseOrderShowBean(int i) {
		
		BCPurchaseOrderShowBean bean = new BCPurchaseOrderShowBean();
		
		if (getIttl_rowOrderOfStkReq()[i]!=null) {
			String a = getIttl_rowOrderOfStkReq()[i];
			if(a==null || a.equals("")) {
				a = "0";
			}
			bean.setIttl_rowOrderOfStkReq(Integer.valueOf(a));
		}
		
		bean.setLineNumber(getLineNumber()[i]);
		bean.setItemCode(getItemCode()[i]);
		try {bean.setItemName(new String(getItemName()[i].getBytes("ISO8859_1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {e.printStackTrace();}		
		
		bean.setUnitCode(getUnitCode()[i]);
		bean.setQty(getQty()[i]);
		bean.setPrice(getPrice()[i]);					
		bean.setLineItemDiscountAmount(getLineItemDiscountAmount()[i]);
		bean.setAmount(getAmount()[i]);
		bean.setStkReqNo(getStkReqNo()[i]);
		bean.setConfirmNo(getConfirmNo()[i]);
		bean.setLineItem_whCode(getLineItem_whCode() [i]);
		bean.setLineItem_shelfCode( getLineItem_shelfCode()[i]);
		bean.setLineItem_statusReceive(getLineItem_statusReceive()[i]);
		
		bean.setLineItem_departCode(getLineItem_departCode()[i]);	
		bean.setLineItem_projectCode(getLineItem_projectCode()[i]);
		bean.setLineItem_allocateCode(getLineItem_allocateCode()[i]);
		try {bean.setLineItem_myDescription(new String(getLineItem_myDescription()[i].getBytes("ISO8859_1"),"UTF-8"));					
		} catch (UnsupportedEncodingException e) {e.printStackTrace();}					
		
		bean.setLineItem_poRefNo(getLineItem_poRefNo()[i]);						
		bean.setLineItem_netAmount(getLineItem_netAmount()[i]);
		bean.setLineItem_homeAmount(getLineItem_homeAmount()[i]);
																																										
		return bean;
	}		
}
